﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using NS.Utilities;
using NS.BLL.Systems;
using NS.Model.Systems;

public partial class Systems_UserModify : System.Web.UI.Page
{
    private string UserID = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        UserID = Request.QueryString["UserID"];
        if (!IsPostBack)
        {
            InitData();
            LoadData();
        }
    }
    public void InitData()
    {
        ddlDept.DataSource = DeptService.GetList(CookieHelper.GetValue("companyid"), 0);
        ddlDept.DataTextField = "DeptName";
        ddlDept.DataValueField = "DeptID";
        ddlDept.DataBind();
    }
    protected void LoadData()
    {
        UserDetail myUser = UserDetailService.Select(UserID);
        HidUserID.Value = UserID;
        txtMobile.Text = myUser.Mobile;
        txtTrueName.Text = myUser.TrueName;
        txtWeChat.Text = myUser.WeChat;
        ddlSex.SelectedValue = myUser.Sex;
        txtCardNumber.Text = myUser.CardNumber;
        txtBirthday.Text = StringHelper.IsConvertToDateTime(myUser.Birthday) ? DateTime.Parse(myUser.Birthday).ToString("yyyy-MM-dd") : DateTime.Now.ToString("yyyy-MM-dd");
        ddlDept.SelectedIndex = ddlDept.Items.IndexOf(ddlDept.Items.FindByValue(myUser.DeptID.ToString()));
        txtPaiXu.Text = myUser.OrderNum.ToString();
        labUserRole.Text = UsersInRolesService.GetUserRoles(UserID);
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        UserDetail UserModel = UserDetailService.Select(UserID);
        UserModel.Mobile = txtMobile.Text.Trim();
        UserModel.TrueName = txtTrueName.Text.Trim();
        UserModel.Sex = ddlSex.SelectedValue;
        UserModel.CardNumber = txtCardNumber.Text.Trim();
        UserModel.Birthday = txtBirthday.Text;
        UserModel.DeptID = int.Parse(ddlDept.SelectedItem.Value);
        UserModel.OrderNum = txtPaiXu.Text;

        if (UserDetailService.Update(UserModel) > 0)
        {
            MessageBox.AppendScript(this, "$(':input[id$=\"btnSearch\"]', window.parent.document.body).click();");
        }
        else
        {
            MessageBox.ShowSaveError(this);
        }
    }
}